package com.smcdd.cdb.dbo.cdb_dbo.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Picture;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者：Administrator on 2017/6/19 15:30
 * 邮箱：zhanghuaiha@gmail.com
 */

public class MianGridAdapter extends BaseAdapter {
    private Context mContext;
    private List<Picture> mList;
    private LayoutInflater inflater;
    /**
     * 布局类型的数量
     */
    private final int VIWE_TYPE_COUNT = 2;
    private final int TYPE_ONE = 0;
    private final int TYPE_TWO = 1;

    public MianGridAdapter(Context context){
        mContext = context;
        inflater = LayoutInflater.from(context);
        mList = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return mList == null? 0:mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return VIWE_TYPE_COUNT;
    }

    @Override
    public int getItemViewType(int position) {
        if (mList != null) {
            if (mList.get(position).getNickname().equals("扫码充电")) {
                return TYPE_TWO;
            }else {
                return TYPE_ONE;
            }

        }
        return TYPE_ONE;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        ViewHolderTwo viewHolderTwo = null;
        int type = getItemViewType(position);
        if (convertView == null) {
            switch (type) {
                case TYPE_ONE:
                    convertView = inflater.inflate(R.layout.item_picture,parent, false);
                    viewHolder = new ViewHolder();
                    viewHolder.title = (TextView) convertView.findViewById(R.id.text_item);
                    viewHolder.image = (ImageView) convertView.findViewById(R.id.image_item);
                    convertView.setTag(viewHolder);
                    break;
                case TYPE_TWO:
                    convertView = inflater.inflate(R.layout.item_picture_two,parent, false);
                    viewHolderTwo = new ViewHolderTwo();
                    viewHolderTwo.title = (TextView) convertView.findViewById(R.id.text_item);
                    viewHolderTwo.two = (TextView) convertView.findViewById(R.id.text_two);
                    viewHolderTwo.image = (ImageView) convertView.findViewById(R.id.image_item);
                    convertView.setTag(viewHolderTwo);
                    break;
            }
        } else {
            switch (type) {
                case TYPE_ONE:
                    viewHolder = (ViewHolder) convertView.getTag();
                    break;
                case TYPE_TWO:
                    viewHolderTwo = (ViewHolderTwo) convertView.getTag();
                    break;
                default:
                    break;
            }
        }
        switch (type) {
            case TYPE_ONE:
            viewHolder.title.setText(mList.get(position).getNickname());
            viewHolder.image.setImageResource(mList.get(position).getIconId());
                break;
            case TYPE_TWO:
            viewHolderTwo.title.setText(mList.get(position).getNickname());
            viewHolderTwo.image.setImageResource(mList.get(position).getIconId());
            viewHolderTwo.two.setText("五分钟");
                break;
            default:
                break;
        }

        return convertView;
    }


    class ViewHolder {
        public TextView title;
        public ImageView image;
    }

    class ViewHolderTwo {
        public TextView title;
        public TextView two;
        public ImageView image;
    }

    public void addData(List<Picture> list){
        if (mList != null) {
            mList.clear();
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }
}
